import time

from playwright.sync_api import sync_playwright
time=time.strftime('%Y%m%d%H%M%S',time.localtime())

def driver():
    with sync_playwright()as p:

        bowers=p.firefox.launch(headless=False)
        page=bowers.new_page()
        page.goto('https://www.jiuxian.com/')
        yield page
        page.close()
        try:
            #点击去登录
            page.click('xpath=/html/body/div[2]/div/div[1]/div/a[1]')
            time.sleep(4)
            #断言
            dy=page.locator('帐号登录').text_content()
            assert '帐号登录' == dy
            #输入账号
            page.fill(selector='#userName',value='13381144084')
            #输入密码
            page.fill(selector='#password', value='grq200566++')
            time.sleep(15)
            #点击登录按钮
            page.click('xpath=/html/body/div/form/div/div/div/div/div[1]/div[2]/div[4]/input')
            time.sleep(5)
            #点击搜索框输入
            page.fill(selector='#wd',value='五粮液')
        except Exception as e:
            #含有时间戳截图
            page.screenshot('jt'+ time + '.png')
            # 点击搜索按钮
            page.click('xpath = /html/body/div[4]/div/div[2]/div[1]/form/input[2]')
            time.sleep(5)
